//
//  SWSingleMeal.m
//  YiYueHairdresser
//
//  Created by Sky on 2018/10/29.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "SWSingleMealView.h"
#import "SWMenueView.h"

@interface SWSingleMealView()<UITableViewDelegate, UITableViewDataSource,UITextFieldDelegate>

@property (nonatomic, strong) UITextField *nameTxt;
@property (nonatomic, strong) UITextField *numTxt;
@property (nonatomic, strong) UITextField *priceTxt;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong)  NSArray <NSArray *>*titleArray;
@property (nonatomic, strong) SWMenueView *menueView;       //服务菜单列表视图
@property (nonatomic, strong) UIButton *saveBtn;            //保存
@end

@implementation SWSingleMealView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.serviceModel = [STServiceInfoModel new];
        [self initUI];
    }
    return self;
}

#pragma mark - 初始化UI
- (void)initUI {
    
    [self addSubview:self.tableView];
    [self addSubview:self.saveBtn];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.inset(0);
        make.bottom.equalTo(self.saveBtn.mas_top);
    }];
    [self.saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.inset(0);
        make.height.mas_equalTo(50);
    }];
    [self.tableView reloadData];
}

-(void)reloadSignleTableView{
    [self.menueView setServiceArray:self.arrServiceType];
    int selNum = -1;
    for (int i = 0; i< self.arrServiceType.count; i++) {
        STServiceCategoryModel * model = self.arrServiceType[i];
        if(self.serviceModel.categoryId == model.objectId){
            selNum = i;
            break;
        }
    }
    if (selNum>=0) {
      [self.menueView setBtn:selNum selStatus:YES];
    }
    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.titleArray.count;
    }
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0|| indexPath.section == 2) {
        return 50;
    }
    return self.menueView.height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return 50;
    }
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if (section == 2) {
        return 50;
    }
    return 0.001;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * addView = [[UIView alloc]init];
    if (section == 2) {
        UILabel * nameLab = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 200, 50)];
        nameLab.font = FONT(14);
        nameLab.textColor = [UIColor lightGrayColor];
        nameLab.text = @"套餐价格=服务价格x次数x0.88";
        [addView addSubview:nameLab];
    }
    return addView;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * headerView = [UIView new];
    headerView.backgroundColor = [UIColor clearColor];
    if (section == 1) {
        
        UILabel * nameLab = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 100, 50)];
        nameLab.font = FONT(17);
        nameLab.textColor = [UIColor lightGrayColor];
        nameLab.text = @"关联服务";
        [headerView addSubview:nameLab];
    }
   
    return headerView;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell;
    if(indexPath.section == 0 || indexPath.section == 2){
        cell = [[NSBundle mainBundle] loadNibNamed:@"STTextCell" owner:nil options:nil].lastObject;
        UILabel * nameLab = (UILabel *)[cell viewWithTag:11001];
        UIView * line = (UIView *)[cell viewWithTag:11003];
        
        if (indexPath.section == 0) {
            line.hidden = indexPath.row == 0?NO:YES;
            nameLab.text = self.titleArray[indexPath.row][0];
            if (indexPath.row ==0) {
                
                _nameTxt = (UITextField *)[cell viewWithTag:11002];
                _nameTxt.delegate = self;
                _nameTxt.placeholder = self.titleArray[indexPath.row][1];
                _nameTxt.text = self.serviceModel.servicename;
            }else{
                _numTxt = (UITextField *)[cell viewWithTag:11002];
                _numTxt.delegate = self;
                if(self.serviceModel.times){
                    _numTxt.text = [NSString stringWithFormat:@"%d",self.serviceModel.times];
                }
                
            }
        } else {
            nameLab.text = @"套餐价格：";
            line.hidden = YES;
            _priceTxt = (UITextField *)[cell viewWithTag:11002];
            _priceTxt.delegate = self;
            if(self.serviceModel.priceType ==2){
                if(self.serviceModel.fromPrice){
                    _priceTxt.text = [NSString stringWithFormat:@"%0.2f-%0.2f",self.serviceModel.fromPrice,self.serviceModel.toPrice];
                }
            }else{
                if(self.serviceModel.price){
                    _priceTxt.text = [NSString stringWithFormat:@"%0.2f",self.serviceModel.price];
                }
            }
        }
        
    } else {
        static NSString *identifier = @"SWMenueView";
        cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
            [cell addSubview:self.menueView];
        }
    }
    cell.selectionStyle= UITableViewCellSelectionStyleNone;
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && indexPath.row == 0) {
        
    }
}


- (void)textFieldDidEndEditing:(UITextField *)textField{
    if([textField isEqual:_nameTxt]){
        self.serviceModel.servicename = textField.text;
    }else if([textField isEqual:_numTxt]){
        self.serviceModel.times = [textField.text intValue];
    }else if([textField isEqual:_priceTxt]){
        self.serviceModel.price = [textField.text floatValue];
        if(!self.serviceModel.costprice){
            self.serviceModel.costprice = [textField.text floatValue];
        }
    }
        
}




- (void)saveAction:(UIButton *)sender {
    self.saveBtnAction(self.serviceModel);
}

#pragma mark - 懒加载
-(UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [UITableView new];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = UIColor.groupTableViewBackgroundColor;
    }
    return _tableView;
}

- (SWMenueView *)menueView {
    if (_menueView == nil) {
        _menueView = [[SWMenueView alloc]initWithFrame:CGRectMake(0, 0, self.width, 0)];
        _menueView.BackSelectBlock = ^(int selNum) {
            STServiceCategoryModel * model = self.arrServiceType[selNum];
            self.serviceModel.packageType =1;
             self.serviceModel.categoryId =model.objectId;
        };
    }
    return _menueView;
}

-(NSArray<NSArray *> *)titleArray {
    if (_titleArray == nil) {
        
        _titleArray = @[@[@"套餐名称：", @"请输入套餐名称"],@[@"使用次数：", @"请输入使用次数"]];
    }
    return _titleArray;
}

-(UIButton *)saveBtn {
    if (_saveBtn == nil) {
        _saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_saveBtn setTitle:@"保存" forState:UIControlStateNormal];
        [_saveBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        _saveBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        _saveBtn.backgroundColor = UIColor.orangeColor;
        [_saveBtn addTarget:self action:@selector(saveAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveBtn;
}
@end
